const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hireTabIndex:0,
    id:'',//雇佣ID
    hireDetail:'',
    taskTabType:1,
    comment_to_he:'',
    comment_to_me:"",
    btnText:'',
    btnFunName:'',
    btnShow:false,
  },

  optionHand(){
    if(this.data.btnFunName){
      this[this.data.btnFunName]()
    }
  },


  // 底部列表切换
  taskTabBtn:function(e){
    console.log(e,'eeee--------')
    const { index } = e.currentTarget.dataset
    this.setData({
      taskTabType: index
    })
  },
  // 雇佣tabhireDetail
  hireTab:function(e){
    const { index } = e.currentTarget.dataset
    this.setData({
      hireTabIndex: index
    })
    this.hireDetail();
  },

  btnChange(data){
    let _this=this
    switch (data.button_status){
      case 7:
        _this.setData({
          btnText:'处理委托',
          btnFunName:'delegateBtn',
          btnShow:true,
        })
        break;
      case 8:
        _this.setData({
          btnText:'交付稿件',
          btnFunName:'upWorks',
          btnShow:true,
        })
        break;
      default:
        _this.setData({
          btnText:'',
          btnFunName:'',
          btnShow:false,
        })

    }
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })

  },
  // 雇佣详情
  hireDetail:function(){
    var {id} = this.data
    var _this = this
    var url = app.globalData.baseUrl + 'user/employDetail';
    var data = {
      employ_id: id,
      token: app.globalData.token
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data, (res) => {
      wx.hideLoading()
      if (res.code == 1000) {
        _this.setData({
          hireDetail:res.data,
          comment_to_he:res.data.comment_to_he,
          comment_to_me:res.data.comment_to_me,
        })
        _this.btnChange(res.data)
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 处理委托
  delegateBtn:function(){
    var { hireDetail, id } = this.data
    var _this = this
    var data = {
      employ_id: id,
      type:'',
      token: app.globalData.token
    }
    // hireDetail.type ==1我是雇主，==2我是威客
    if (hireDetail.type == 1){
      wx.showModal({
        title: '提示',
        content: '您确定要取消雇佣吗',
        success(res) {
          if (res.confirm) {
            data.type = 1
            _this.delegateFun(data);
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '待受理状态',
        cancelText: '拒绝',
        confirmText: '受理',
        success(res) {
          if (res.confirm) {
            data.type = 2
            _this.delegateFun(data);
          } else if (res.cancel) {
            data.type = 3
            _this.delegateFun(data);
          }
        }
      })
    }

  },
  // 处理委托方法
  delegateFun: function (data){
    var _this = this
    var url = app.globalData.baseUrl + 'user/dealEmploy';
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data, (res) => {
      wx.hideLoading()
      if (res.code == 1000) {
        _this.hireDetail();
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 上传作品
  upWorks:function(){
    var { hireDetail } = this.data
    wx.navigateTo({
      url: '/pages/hireDeliver/hireDeliver?id=' + hireDetail.employ.id,
    })
  },
  // 雇主通过验收
  acceptanceBtn:function(){
    var { hireDetail } = this.data
    var _this = this
    var data = {
      employ_id: hireDetail.employ.id,
      token: wx.getStorageSync('token')
    }
    wx.showModal({
      title: '提示',
      content: '请确认您是否已查看源文件，并通过验收！',
      success(res) {
        if (res.confirm) {
          var url = app.globalData.baseUrl + 'user/acceptEmployWork';
          wx.showLoading({
            title: '加载中',
          })
          app.wxPostRequest(url, data, (res) => {
            wx.hideLoading()
            if (res.code == 1000) {
              _this.hireDetail();
            }
          }, (err) => {
            wx.hideLoading()
            wx.showToast({
              title: err.data.message,
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {}
      }
    })
  },
  wxdown:function(event){
    var { down_url, annextype} = event.currentTarget.dataset;
    console.log(down_url, annextype)
    let _that = this;
    if (annextype != 'jpg' && annextype != 'png' && annextype != 'jpeg' && annextype != 'gif'){
      if (annextype != 'zip' && annextype != 'rar'){
        wx.downloadFile({
          url: down_url,
          success: function (res) {
            const tempFilePath = res.tempFilePath;
            // 保存文件
            wx.saveFile({
              tempFilePath,
              success: function (res) {
                const savedFilePath = res.savedFilePath;
                // 打开文件
                wx.openDocument({
                  filePath: savedFilePath,
                  showMenu:true,
                  success: function (res) {
                    console.log('打开文档成功')
                  },
                });
              },
              fail: function (err) {
                console.log('保存失败：', err)
              }
            });
          },
          fail: function (err) {
            console.log('下载失败：', err);
          },
        });
      }else{
        wx.showToast({
          title: '暂不支持改文件类型',
          icon: "none",
          duration: 2000
        })
      }
      return false;
    }else{
      //预览
      wx.previewImage({
        current: down_url, // 当前显示图片的http链接
        urls:[
          down_url
        ]
      })


    /*  wx.showToast({
        icon: 'loading',
        title: '正在保存图片',
        duration: 1000
      })
      //判断用户是否授权"保存到相册"
      wx.getSetting({
        success(res) {
          //没有权限，发起授权
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {//用户允许授权，保存图片到相册
                _that.savePhoto(down_url);
              },
              fail() {//用户点击拒绝授权，跳转到设置页，引导用户授权
                wx.openSetting({
                  success() {
                    wx.authorize({
                      scope: 'scope.writePhotosAlbum',
                      success() {
                        _that.savePhoto(down_url);
                      }
                    })
                  }
                })
              }
            })
          } else {//用户已授权，保存到相册
            _that.savePhoto(down_url);
          }
        }
      })*/
    }
  },

  savePhoto(down_url) {
    wx.downloadFile({
      url: down_url,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: "success",
              duration: 1000
            })
          }
        })
      }
    })
  },



  // 评价
  evaluation:function(){
    var { hireDetail } = this.data
    // hireDetail.type ==1我是雇主，==2我是威客
    var type = '';
    if (hireDetail.type == 1){
      type = 2
    }else{
      type = 1
    }
    //跳转评论 参数type 2是雇主 1是威客
    wx.navigateTo({
      url: '/taskPage/pages/taskEvaluation/taskEvaluation?type=' + type + '&employ_id=' + hireDetail.employ.id + '&title=' + hireDetail.employ.title + '&avatar=' + hireDetail.avatar + '&evaluationType=1',
    })
  },
  // 维权
  protectionBtn:function(e){
    var { hireDetail } = this.data
    wx.navigateTo({
      url: '/taskPage/pages/protection/protection?protectionType=1' + '&id=' + hireDetail.employ.id,
    })
  },
  // 联系
  contactBtn: function () {
    var { hireDetail } = this.data;
    wx.makePhoneCall({
      phoneNumber: hireDetail.employ.phone
    })
  },
  // 店铺
  shopBtn:function(){
    var { hireDetail } = this.data;
    wx.navigateTo({
      url: '/taskPage/pages/talentDetail/talentDetail?id=' + hireDetail.shop_id + '&shopType=1',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.hireDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
